﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
public partial class active_WithMe_admin_index : System.Web.UI.Page
{
    protected XmlNodeList xnl = null;
    protected DataTable HotRoomOrder = null;
    protected XmlNodeList BoyAndGirl = null;
    protected string message = string.Empty;
    public static string FlashGameDBConnstring = getsql.getstr("activestr");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["user"] == null || Session["user"].ToString().Length<=0)
        {
            Response.Redirect("login.aspx");
            Response.End();
        }

        if (Request.QueryString["delete"]!=null&&Request.QueryString["delete"].ToString()== "yes")
        {

            DeleteContext();
        }
        if (Session["message"] != null && Session["message"].ToString().Length > 0)
        {
            message = Session["message"].ToString();
            Session["message"] = "";
        }
        init();

    }


    private void init()
    {
        XmlDocument xml = LoadXml("../active.xml");
        XmlNode xn = xml.SelectSingleNode("Active");
        if (xn != null)
            xnl = xn.ChildNodes;


    }

    protected void SignOut(object sender, EventArgs e)
    {
        Session["user"] = "";
        Response.Redirect("login.aspx");
    }



    //load xml file
    private XmlDocument LoadXml(string FileName)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(Server.MapPath(FileName));
        return xmlDoc;
    }





    public void EditContent(object sender, EventArgs e)
    {
        if (Request.Form["name"] == null || Request.Form["date-input"] == null || Request.Form["time-input"] == null || Request.Form["roomid-input"] == null
             || Request.Form["date-input"].ToString().Length <= 0 || Request.Form["time-input"].ToString().Length <= 0 || Request.Form["roomid-input"].Length<=0)
        {
            Session["message"] = "NoComplete";
            Response.Redirect("index.aspx");
            return;
        }
        try
        {
            XmlDocument xml = LoadXml("../active.xml");
            XmlNode xn = xml.SelectSingleNode("Active");
            if (xn != null)
                xnl = xn.ChildNodes;
            if (xnl == null)
            {
                return;
            }
            if (Request.Form["name"].ToString().Length > 0)
            {
                foreach (XmlNode xf in xnl)
                {

                    XmlElement xe = (XmlElement)xf;

                    if (xe.GetAttribute("name") == Request.Form["name"].ToString())
                    {

                        xe.SetAttribute("dance", Request.Form["date-input"].ToString());
                        xe.SetAttribute("teacher", Request.Form["roomid-input"].ToString());
                        xe.SetAttribute("time", Request.Form["time-input"].ToString());

                        break;

                    }

                }
            }
            else
            {
                XmlElement xe = xml.CreateElement("ActiveInfo");

                long i = long.Parse(xnl[xnl.Count - 1].Attributes["name"].Value) + 1;
                xe.SetAttribute("name", i.ToString());
                xe.SetAttribute("dance", Request.Form["date-input"].ToString());
                xe.SetAttribute("teacher", Request.Form["roomid-input"].ToString());
                xe.SetAttribute("time", Request.Form["time-input"].ToString());
                xn.AppendChild(xe);
            }

            xml.Save(Server.MapPath("../active.xml"));
     
        }
        catch (Exception ex)
        {
            Session["message"] = "error";
            Response.Redirect("index.aspx");
            //Response.Write(ex.Message.ToString());
            return;
        }
        Session["message"] = "success";
        Response.Redirect("index.aspx");


    }

    private void DeleteContext()
    {
        if (Request.QueryString["id"] == null || Request.QueryString["id"].ToString().Length<=0)
        {

            return;
        }
        try
        {
            XmlDocument xml = LoadXml("../active.xml");
            XmlNode xn = xml.SelectSingleNode("Active");
            if (xn != null)
                xnl = xn.ChildNodes;
            if (xnl == null)
            {
                return;
            }

            if (Request.QueryString["id"].ToString().Length > 0)
            {
                foreach (XmlNode xf in xnl)
                {

                    XmlElement xe = (XmlElement)xf;

                    if (xe.GetAttribute("name") == Request.QueryString["id"].ToString())
                    {

                        xn.RemoveChild(xf);

                        break;

                    }

                }

                xml.Save(Server.MapPath("../active.xml"));
 
            }
        }
        catch
        {
            Session["message"] = "error";
            Response.Redirect("index.aspx");
            return;
        }
        Session["message"] = "success";
        Response.Redirect("index.aspx");


    }



}
